package com.jy.adscm.operation.hr.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.operation.hr.bean.HrResumePicture;
import com.jy.adscm.operation.hr.mapper.mysql.HrResumePictureMapper;
import com.jy.adscm.operation.hr.service.HrResumePictureService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2020-01-07
 */
@Service
public class HrResumePictureServiceImpl extends ServiceImpl<HrResumePictureMapper, HrResumePicture> implements HrResumePictureService {
    @Value("${ly.upload.imagePath}")
    private  String imagePath;
    @Value("${ly.upload.imageUrl}")
    private  String imageUrl;
    @Autowired
    JyUserMapper jyUserMapper;
    @Override
    public boolean insertHrResumePicture(HrResumePicture hrResumePicture) {
        hrResumePicture.setPath(hrResumePicture.getPath().replace(imageUrl,""));
        boolean insert = super.insert(hrResumePicture);
        return false;
    }

    @Override
    public boolean deleteHrResumePicture(Integer id) {
        HrResumePicture hrResumePicture = super.selectById(id);
        String path=imagePath+hrResumePicture.getPath();
        File file =new File(path);
        if (file.exists()){
            file.delete();
        }
        boolean b = super.deleteById(id);
        return b;
    }

    @Override
    public List<HrResumePicture> selectByRid(Integer rid) {
        Wrapper<HrResumePicture> hrResumePictureWrapper=new EntityWrapper<>();
        hrResumePictureWrapper.eq("rid",rid);
        List<HrResumePicture> hrResumePictures = super.selectList(hrResumePictureWrapper);
        for (HrResumePicture hrResumePicture : hrResumePictures) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(hrResumePicture.getPath(),null)){
                hrResumePicture.setPath(imageUrl+hrResumePicture.getPath());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(hrResumePicture.getCreator(),null)){
                JyUser jyUser = jyUserMapper.selectById(hrResumePicture.getCreator());
                hrResumePicture.setCreatorName(jyUser.getName());
            }
        }
        return hrResumePictures;
    }


}
